package com.jxf.demo.service.impl;

import com.jxf.demo.dto.*;
import com.jxf.demo.service.AbstractDXFParseService;
import org.kabeja.dxf.DXFCircle;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DXFCircleParseService extends AbstractDXFParseService {
    @Override
    public List<DXFPointInfo> doParse(List lineEntities) {
        return parseCircle(lineEntities);
    }

    @Override
    public String getType() {
        return "CIRCLE";
    }

    /**
     * 圆形参数获取
     *
     * @param circleEntities
     */
    public List<DXFPointInfo> parseCircle(List circleEntities) {
        List<DXFPointInfo> circleInfoList = new ArrayList<>();
        for (Object circleEntity : circleEntities) {
            DXFCircle circle = (DXFCircle) circleEntity;
            // 圆半径
            double radius = circle.getRadius();
            // 圆坐标
            StraightLine line = new StraightLine();
            line.setStart(StraightLine.Coordinate.getPointCoordinate(circle.getCenterPoint().getX(),circle.getCenterPoint().getY()));
//            circlePoint.setCenter(circle.getCenterPoint());
            line.setRadius(BigDecimal.valueOf(radius));
            // 根据坐标和半径计算外接矩形宽高
            DXFPointInfo circleInfo = DXFPointInfo.builder()
                    .type("CIRCLE")
                    .points(Collections.singletonList(line))
                    .build();
            BigDecimal diameter = BigDecimal.valueOf(radius).multiply(new BigDecimal("2"));
//            // 外接矩形
//            DXFBoundingRectangle boundingRectangle = DXFBoundingRectangle.builder()
//                    .width(diameter.doubleValue())
//                    .height(diameter.doubleValue())
//                    // 圆心X轴-半径为X轴起点坐标
//                    .x(circlePoint.getCenter().getX() - circlePoint.getRadius())
//                    // 圆心Y轴-半径为Y轴起点坐标
//                    .y(circlePoint.getCenter().getY() - circlePoint.getRadius()).build();
//

            BigDecimal widthHeight = new BigDecimal(Double.toString(diameter.doubleValue()));
            Rectangle rectangle = Rectangle.builder()
                    .widthHeight(new BigDecimal[]{widthHeight, widthHeight})
                    .realWidthHeight(new BigDecimal[]{widthHeight, widthHeight})
                    .build();
            circleInfo.setRectangle(rectangle);
            circleInfoList.add(circleInfo);
        }
        return circleInfoList;
    }
}
